package com.jstyle.beatecg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.jstyle.beatecg.R;
import com.jstyle.beatecg.adapter.WheelAdapter;
import com.jstyle.beatecg.base.BaseActivity;
import com.jstyle.beatecg.daoManager.UserInfoDaoManager;
import com.jstyle.beatecg.model.UserInfo;
import com.jstyle.beatecg.util.GlobalConast;
import com.jstyle.beatecg.util.ScreenUtils;
import com.jstyle.beatecg.util.SharePreferenceUtils;
import com.jstyle.beatecg.view.EditTextWithDelete;
import com.jstyle.beatecg.view.widget.views.WheelView;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加使用者设置信息
 */
public class UserInfoSettingActivity extends BaseActivity {

    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    @BindView(R.id.bt_back)
    ImageView btBack;
    @BindView(R.id.tv_set_userinfo_title)
    TextView tvSetUserinfoTitle;
    @BindView(R.id.tv_set_userinfo_tips)
    TextView tvSetUserinfoTips;
    @BindView(R.id.wheelView_setuserinfo_1)
    WheelView wheelViewSetuserinfo1;
    @BindView(R.id.et_setuserinfo_name)
    EditTextWithDelete etSetuserinfoName;
    @BindView(R.id.bt_setuserinfo_next)
    Button btSetuserinfoNext;

    List<String> genderList = new ArrayList<>();
    List<String> ageList = new ArrayList<>();
    List<String> heightList = new ArrayList<>();
    List<String> weightList = new ArrayList<>();
    String[] titleArrays;
    String[] tipsArrays;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info_setting);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    private void initData() {
        tipsArrays = getResources().getStringArray(R.array.set_userinfo_tips);
        titleArrays = getResources().getStringArray(R.array.set_userinfo_title);
        genderList.add(getString(R.string.male));
        genderList.add(getString(R.string.female));
        weightList = getListData(20, 221);
        heightList = getListData(100, 231);
        ageList = getListData(5, 131);
    }

    private List<String> getListData(int start, int end) {
        List<String> list = new ArrayList<>();
        for (int i = start; i < end; i++) {
            list.add(String.format("%02d", i));
        }
        return list;
    }


    private void initView() {
        ScreenUtils.setStatusBarColor(llTitle, getResources().getColor(R.color.colorPrimary));

        WheelAdapter wheelAdapter = new WheelAdapter(this, ageList, 0, 24, 14);
        wheelViewSetuserinfo1.setViewAdapter(wheelAdapter);

    }

    int count = 0;

    @OnClick({R.id.bt_back, R.id.bt_setuserinfo_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.bt_setuserinfo_next:
                if (count == 4) {
                    int heightItem=wheelViewSetuserinfo1.getCurrentItem();
                    String heightS=ageList.get(heightItem);
                    age=Integer.valueOf(heightS);
                    Intent intent =new Intent(this,BindDeviceActivity.class);
                    intent.putExtra(GlobalConast.KEY_isFirst,true);
                    SharePreferenceUtils.setSpBoolean(GlobalConast.KEY_isFirst,false);
                    saveUserInfo();
                    startActivity(intent);
                    finish();
                } else {
                    changeUserInfo();

                }
                break;
        }
    }

    private static final String TAG = "UserInfoSettingActivity";
    private void saveUserInfo() {
        Log.i(TAG, "saveUserInfo: "+age+" "+gender+" "+height+" "+weight+" "+nickName);
        String account=SharePreferenceUtils.account_default;
        UserInfo userInfo= UserInfoDaoManager.getUser(account);
        if(userInfo==null){
            userInfo=new UserInfo();
            userInfo.setPhone(account);
        }
        userInfo.setAge(age);
        userInfo.setGender(gender);
        userInfo.setHeight(height);
        userInfo.setWeight(weight);
        userInfo.setName(nickName);
        UserInfoDaoManager.insertUser(userInfo);



    }
    int age;
    int gender;
    int height;
    int weight;
    String nickName;
    private void changeUserInfo() {
        WheelAdapter wheelAdapter = null;
        if (count == 0) {
            nickName=etSetuserinfoName.getText().toString();
            if (TextUtils.isEmpty(nickName)) {
                showToast(getString(R.string.fill_all_info));
                return;
            }
            wheelViewSetuserinfo1.setVisibility(View.VISIBLE);
            etSetuserinfoName.setVisibility(View.GONE);
            wheelAdapter = new WheelAdapter(this, genderList, 0, 24, 14);
        } else if (count == 1) {
            gender=wheelViewSetuserinfo1.getCurrentItem();
            wheelAdapter = new WheelAdapter(this, heightList, 0, 24, 14);
            wheelViewSetuserinfo1.setLaber("cm");
        } else if (count == 2) {
            int heightItem=wheelViewSetuserinfo1.getCurrentItem();
            String heightS=heightList.get(heightItem);
            height=Integer.valueOf(heightS);
            wheelAdapter = new WheelAdapter(this, weightList, 0, 24, 14);
            wheelViewSetuserinfo1.setLaber("kg");
            wheelViewSetuserinfo1.setCurrentItem(45,true);//t体重65
        } else if (count == 3) {
            int heightItem=wheelViewSetuserinfo1.getCurrentItem();
            String heightS=weightList.get(heightItem);
            weight=Integer.valueOf(heightS);
            wheelAdapter = new WheelAdapter(this, ageList, 0, 24, 14);
            wheelViewSetuserinfo1.setLaber(getString(R.string.old));
            wheelViewSetuserinfo1.setCurrentItem(13,true);//十八岁
        }

        changeTv();
        wheelViewSetuserinfo1.setViewAdapter(wheelAdapter);
        if (count == 1) {
            wheelViewSetuserinfo1.setCurrentItem(70,true);//身高默认1米7
        }
        count++;
    }

    private void changeTv() {
        tvSetUserinfoTitle.setText(titleArrays[count]);
        tvSetUserinfoTips.setText(tipsArrays[1]);
    }

}
